<?php
namespace app\index\controller;

use app\BaseController;
use think\facade\Db;
class Index extends BaseController
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">16载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
    }

    public function hello($name = 'ThinkPHP6')
    {
        return 'hello,' . $name;
    }
	
	/**
	 * jt808/2013
	 */
	public function test()
	{
		//$hex_string = input('data');	//设备 二进制 数据
		//$str = bin2hex($hex_string);	//二进制转16进制
		
		//注册报文
		$str = "7E 01 00 00 2D 01 07 43 08 30 14 00 02 00 0A 03 F2 32 32 32 30 31 43 41 2D 38 42 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 33 30 38 33 30 31 34 FF 30 30 30 30 30 30 30 30 18 7E";
		//鉴权报文
		//$str = "7E 01 02 00 06 01 07 43 08 30 14 00 02 31 32 33 34 35 36 69 7E";
		//位置信息报文
		//$str = "7E 02 00 00 28 01 07 43 08 30 14 00 06 00 00 00 00 00 0C 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 21 03 01 15 18 30 01 04 00 00 00 00 30 01 1C 31 01 00 4E 7E";
		
		$strNew = str_replace(' ', '', $str);
		$data = strtoupper($strNew);
		$reply = '';	//应答报文
		
		if (mb_substr($data, 0, 2) == '7E' && mb_substr($data, -2, 2) == '7E') {
			$msgData = mb_substr($data,2,-2);
			$headData = mb_substr($msgData,0,24);
			$bodyData = mb_substr($msgData,24);
			
			$reply = '';								//应答报文
			$MsgID = mb_substr($headData, 0, 4);		//消息ID 0100
			$MsgLength = mb_substr($headData, 4, 4);	//消息体属性
			$DeviceID = mb_substr($headData, 8, 12);	//终端号 010743083014
			$FlowID = mb_substr($headData, 20, 4);		//流水号
			
			$Auth = '';		//鉴权
			$Code = '';		//应答码
			$GPS = '';
			
			print_r('消息ID：');print_r($MsgID);echo"<br>";
			print_r('消息体属性：');print_r($MsgLength);echo"<br>";
			print_r('终端号：');print_r($DeviceID);echo"<br>";
			print_r('流水号：');print_r($FlowID);echo"<br>";
			print_r('消息体：');print_r($bodyData);echo"<br>";

			if ($MsgID == '0100') {
				//终端注册
				$ProvinceID = mb_substr($bodyData, 0, 4);//省域ID
				$CityID = mb_substr($bodyData, 4, 4);//市县域ID
				$CompanyID = mb_substr($bodyData, 8, 10);//制造商ID
				$TerminalModel = mb_substr($bodyData, 18, 40);//终端型号
				$TerminalId = mb_substr($bodyData, 58, 14);//终端ID
				$PlateColor = mb_substr($bodyData, 72, 2);//车牌颜色
				$PlateCode = mb_substr($bodyData, 74, 18);//车牌
				
				/**** 业务 start ****/
				// ...
				/**** 业务 over ****/
				
				
				$Code = '8100';							//应答消息ID
				$Auth = bin2hex($DeviceID);				//鉴权码
				
				/********应答组装********/
				//第一步：封装消息体 = 应答流水号 + 应答结果 + 鉴权码
				$MsgBody = $FlowID . '00' . $Auth;
			} else if ($MsgID == '0102') {
				//终端鉴权
				
				$Code = '8001';							//应答消息ID
				/********应答组装********/
				//第一步：封装消息体 = 应答流水号 + 应答消息ID + 应答结果
				$MsgBody = $FlowID . $MsgID . '00';
				
			} else if ($MsgID == '0200') {
				//终端位置上报
				$mark = mb_substr($bodyData, 0, 8);//标识
				$status = mb_substr($bodyData, 8, 8);//状态
				$Lat = mb_substr($bodyData, 16, 8);//纬度
				$Lng = mb_substr($bodyData, 24, 8);//经度
				$Lat = hexdec($Lat) / 1000000;//十六进制转10进制 北斗(84)坐标纬度
				$Lng = hexdec($Lng) / 1000000;//十六进制转10进制 北斗(84)坐标经度
				$height = mb_substr($bodyData, 32, 4);//高
				$speed = mb_substr($bodyData, 36, 4);//速度
				$direction = mb_substr($bodyData, 40, 4);//方向
				$datetime = mb_substr($bodyData, 44,12);
				$time = "20" . $datetime;//时间
				
				/**** 业务 start ****/
				// ...
				/**** 业务 over ****/
				
				$Code = '8001';							//应答消息ID
				/********应答组装********/
				//第一步：封装消息体 = 应答流水号 + 应答消息ID + 应答结果
				$MsgBody = $FlowID . $MsgID . '00';
				
				$GPS = "\n经纬度：" . $Lat . ',' . $Lng;
				$cd_code = mb_substr($DeviceID, 1, 11);
				
			}else {
				$MsgBody = $FlowID . $MsgID . '00';
				$Code = '8001';
			}

			if ($Code) {
				//平台通用应答
				/********应答组装********/
				//第二步：消息体属性(长度) = 消息体长度+保留位+加密方式
				$MsgLen = strtoupper(str_pad(dechex(mb_strlen($MsgBody) / 2), 4, 0, STR_PAD_LEFT));	//消息体属性(长度)
				//第三步：封装需要计算校验码的消息体部分：包含：应答消息ID + 消息体属性(长度) + 设备S/N + 消息流水号 + 第一步中的消息体(应答流水号+应答消息ID+应答结果)
				$Body = $Code . $MsgLen . $DeviceID . $FlowID . $MsgBody;
				//第四步：计算校验码
				$CheckSum = 0;
				for ($i = 0; $i < mb_strlen($Body); $i += 2) {
					$CheckSum ^= hexdec(mb_substr($Body, $i, 2));
				}
				$Check = strtoupper(str_pad(dechex($CheckSum), 2, 0, STR_PAD_LEFT));
				//第五步：组成完整的消息包
				$reply = '7E' . $Body . $Check . '7E';
			}
		}

		//print_r('回复报文：');print_r($reply);die;
		
		//返回二进制报文
		return hex2bin($reply);
	}
	//坐标转换(84转高德)
	public function lnglat()
	{
		$time = date('Ymdhis');
		$lng = '106.696593';
		$lat = '29.51675';
		
		$key = '44407ba247bb1cf6088178b8351dd36f';//高德key
		$map = new \app\org\Map();
		$resAry = $map->wgs84togcj02($lng, $lat);
		$resAry[] = strtotime($time);
		$resAry[] = $map->getGdAddress($key,$resAry[0],$resAry[1]);
		
		echo"<pre>";print_r($resAry);
	}
	
}
